


export default class ColorEvent {
    h = 0
    s = 0
    v = 0
    b = 0
    RGB = '#FF0000'
    constructor(h, s, b) {
        this.h = h;
        this.s = s;
        this.v = b;
    }
    setRGB(Color) {
        this.RGB = Color.substring(0, 7);
        let sanitizedHex = Color.replace("#", "");
        let r = parseInt(sanitizedHex.substring(0, 2), 16);
        let g = parseInt(sanitizedHex.substring(2, 4), 16);
        let b = parseInt(sanitizedHex.substring(4, 6), 16);

        let hue = 0;
        let saturation = 0;
        let brightness = 0;

        let max = Math.max(r, g, b);
        let min = Math.min(r, g, b);
        let delta = max - min;

        brightness = max / 255;
        saturation = delta / 255;

        if (saturation === 0) {
            hue = 0;
        } else {
            let red = (max - r) / delta;
            let green = (max - g) / delta;
            let blue = (max - b) / delta;

            if (r === max) {
                hue = blue - green;
            } else if (g === max) {
                hue = 2 + red - blue;
            } else if (b === max) {
                hue = 4 + green - red;
            }

            hue = hue < 0 ? hue + 6 : hue;
            hue = hue * 60;
        }

        this.h = Math.round(hue);
        this.s = Math.round(saturation * 100);
        this.b = Math.round(brightness * 100);
    }
    toHex(colorValue) {
        const hex = colorValue.toString(16);
        return hex.length == 1 ? "0" + hex : hex;
    };
    setHSB(hue, saturation, brightness) {
        let red, green, blue;
        let h = (hue / 360) * 6;
        let s = saturation / 100;
        let v = brightness / 100;
        let i = Math.floor(h);
        let f = h - i;
        let p = v * (1 - s);
        let q = v * (1 - f * s);
        let t = v * (1 - (1 - f) * s);

        switch (i) {
            case 0:
                red = v;
                green = t;
                blue = p;
                break;
            case 1:
                red = q;
                green = v;
                blue = p;
                break;
            case 2:
                red = p;
                green = v;
                blue = t;
                break;
            case 3:
                red = p;
                green = q;
                blue = v;
                break;
            case 4:
                red = t;
                green = p;
                blue = v;
                break;
            case 5:
                red = v;
                green = p;
                blue = q;
                break;
        }
        this.RGB = "#" + this.toHex(Math.round(red * 255)) + this.toHex(Math.round(green * 255)) + this.toHex(Math.round(blue * 255));
    }
}